import type { UploadUserFile } from 'element-plus'
import { Attachment} from '@/api/common/common'
import CommonUtil from '@/utils/commonUtil'
/**
 * 请求class
 */
class AttachUtil {
 

  /**
   * 获取原来附件保留的id
   * @param url 
   * @param data 
   * @returns 
   */
  public static getKeepIds(oldIds?: number[], files?: UploadUserFile[]): number[] {
    if (!oldIds || oldIds.length == 0) {
      return [];
    }
    if (!files || files.length == 0) {
      return oldIds;
    }
    return oldIds.filter(id => {
      return files.some(file => file.uid == id);
    });
  }

  public static getFilePreview(id:string): string {
    return CommonUtil.getRequestPath() + "/system/attachment/preview?attachmentId=" + id;
  }

    public static getFileDownload(id:string): string {
    return CommonUtil.getRequestPath() + "/system/attachment/download?attachmentId=" + id;
  }

  public static attachToWebFiles(attachments?: Array<Attachment>): Array<UploadUserFile> {
     if (!attachments || attachments.length == 0) {
      return [];
    } 
    const result = attachments.map(f => {
      const fileObj: UploadUserFile = {
        uid: f.id ? Number(f.id) : undefined,
        name: f.name ?? '',
        // url: f.previewUrl,
        url: AttachUtil.getFilePreview("" + f.id),
        status: 'success'
      }
      return fileObj;
    });
    return result;
  }

  public static attachToWebUrl(attachments?: Array<Attachment>): string[] {
     return AttachUtil.attachToWebFiles(attachments).map(f => f.url || '');
  }

  public static getUploadOneUrl() {
      return CommonUtil.getRequestPath() + "/system/attachment/uploadOne";
  }


}

export default AttachUtil
